package com.yzd.otherlearn.jvm;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import com.yzd.otherlearn.util.Print;

public class MethodHandleTest {
	static class ClassA{
		public void println(String s ) {
			Print.print(s);
		}
	}
	public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, Throwable {
		Object obj = System.currentTimeMillis() % 2 == 0 ? System.out : new ClassA();
		getPrintMH(obj).invokeExact("mmmkkk");
	}
	private static MethodHandle getPrintMH(Object receiver) throws NoSuchMethodException, IllegalAccessException {
		
		MethodType mt = MethodType.methodType(void.class, String.class);
		return MethodHandles.lookup().findVirtual(receiver.getClass(), "println", mt).bindTo(receiver);
	}
}
